<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" th:with="title='新闻新增-中楚鸿',active='news'">
<header th:replace="admin/header::headerFragment(${title},${active})"></header>

<link th:href="@{/plugin/ckeditor5/ckeditor.css}" rel="stylesheet"/>
<link th:href="@{/plugin/imageUpload/css/upload.css}" rel="stylesheet"/>
<link th:href="@{/plugin/datetimepicker/css/bootstrap-datetimepicker.min.css}" rel="stylesheet"/>

<body class="fixed-left">
<div id="wrapper">
    <div th:replace="admin/header::header-body"></div>
    <div class="content-page">
        <div class="content">
            <div class="container">
                <div class="row">
                    <div class="col-sm-12">
                        <h4 class="page-title">
                            <a style="font-size: 16px; color: #666666;" href="javascript:void(0)" onclick="window.history.go(-1);"><i class="fa fa-chevron-left" aria-hidden="true"></i>&nbsp;&nbsp;返回</a>
                        </h4>
                    </div>
                    <div class="col-md-12">
                        <form id="newsForm" class="form-horizontal" role="form" novalidate="novalidate">
                            <input type="hidden" name="id" th:value="${news!=null and news.id!=null}?${news.id}: ''" id="id"/>

                            <div class="form-group col-sm-3">
                                <label class="col-sm-4 control-label" for="projectLevel">项目级别</label>
                                <div class="col-sm-8">
                                    <select id="projectLevel" name="projectLevel" class="form-control">
                                        <option value=""> - 请选择项目级别 - </option>
                                        <option value="部委" th:selected="${news!=null and news.projectLevel=='部委'}?true:false">部委</option>
                                        <option value="省级" th:selected="${news!=null and news.projectLevel=='省级'}?true:false">省级</option>
                                        <option value="市级" th:selected="${news!=null and news.projectLevel=='市级'}?true:false">市级</option>
                                        <option value="区级" th:selected="${news!=null and news.projectLevel=='区级'}?true:false">区级</option>
                                    </select>
                                </div>
                            </div>

                            <div class="form-group col-sm-3">
                                <label class="col-sm-4 control-label" for="fundAmount">资助力度</label>
                                <div class="col-sm-8">
                                    <select id="fundAmount" name="fundAmount" class="form-control">
                                        <option value=""> - 请选择资助力度 - </option>
                                        <option value="50万以下" th:selected="${news!=null and news.fundAmount=='50万以下'}?true:false">50万以下</option>
                                        <option value="50-100万" th:selected="${news!=null and news.fundAmount=='50-100万'}?true:false">50-100万</option>
                                        <option value="100-500万" th:selected="${news!=null and news.fundAmount=='100-500万'}?true:false">100-500万</option>
                                        <option value="500万以上" th:selected="${news!=null and news.fundAmount=='500万以上'}?true:false">500万以上</option>
                                    </select>
                                </div>

                            </div>

                            <div class="form-group col-sm-3">
                                <label class="col-sm-4 control-label" for="fundType">资助类型</label>
                                <div class="col-sm-8">
                                    <select id="fundType" name="fundType" class="form-control">
                                        <option value=""> - 请选择资助类型 - </option>
                                        <option value="人才计划" th:selected="${news!=null and news.fundType=='人才计划'}?true:false">人才计划</option>
                                        <option value="技术创新" th:selected="${news!=null and news.fundType=='技术创新'}?true:false">技术创新</option>
                                        <option value="技术改造" th:selected="${news!=null and news.fundType=='技术改造'}?true:false">技术改造</option>
                                        <option value="高技术产业化" th:selected="${news!=null and news.fundType=='高技术产业化'}?true:false">高技术产业化</option>
                                        <option value="企业管理提升" th:selected="${news!=null and news.fundType=='企业管理提升'}?true:false">企业管理提升</option>
                                        <option value="市场拓展" th:selected="${news!=null and news.fundType=='市场拓展'}?true:false">市场拓展</option>
                                        <option value="品牌培育" th:selected="${news!=null and news.fundType=='品牌培育'}?true:false">品牌培育</option>
                                        <option value="税费优惠" th:selected="${news!=null and news.fundType=='税费优惠'}?true:false">税费优惠</option>
                                        <option value="资质认定" th:selected="${news!=null and news.fundType=='资质认定'}?true:false">资质认定</option>
                                        <option value="文化创意" th:selected="${news!=null and news.fundType=='文化创意'}?true:false">文化创意</option>
                                        <option value="其他方向" th:selected="${news!=null and news.fundType=='其他方向'}?true:false">其他方向</option>
                                    </select>
                                </div>
                            </div>

                            <div class="form-group col-sm-3">
                                <label class="col-sm-4 control-label" for="fundMode">资助方式</label>
                                <div class="col-sm-8">
                                    <select id="fundMode" name="fundMode" class="form-control">
                                        <option value=""> - 请选择资助方式 - </option>
                                        <option value="资金" th:selected="${news!=null and news.fundMode=='资金'}?true:false">资金</option>
                                        <option value="政策" th:selected="${news!=null and news.fundMode=='政策'}?true:false">政策</option>
                                        <option value="资金+政策" th:selected="${news!=null and news.fundMode=='资金+政策'}?true:false">资金+政策</option>
                                    </select>
                                </div>
                            </div>

                            <div class="form-group col-sm-3">
                                <label class="col-sm-4 control-label" for="orgDept">组织部门</label>
                                <div class="col-sm-8">
                                    <input id="orgDept" name="orgDept" type="text" class="form-control" placeholder="组织部门"  required="" aria-required="true"
                                           th:value="${news!=null and news.orgDept!=null }?${news.orgDept}: ''"/>
                                </div>
                            </div>

                            <div class="form-group col-sm-3">
                                <label for="applyStartDate" class="col-md-4 control-label">申报开始日期</label>
                                <div class="col-md-8">
                                    <div class="input-group date form_datetime" data-date-format="yyyy-mm-dd" data-link-field="applyStartDateLink">
                                        <input id="applyStartDate" class="form-control" type="text" required="true" aria-required="true" th:value="${news!=null and news.applyStartDate!=null }?${#dates.format(news.applyStartDate,'yyyy-MM-dd')}: ''" readonly>
                                        <span class="input-group-addon"><span class="glyphicon glyphicon-remove"></span></span>
                                        <input type="hidden" id="applyStartDateLink" name="applyStartDate" th:value="${news!=null and news.applyStartDate!=null }?${#dates.format(news.applyStartDate,'yyyy-MM-dd')}: ''" />
                                    </div>
                                </div>
                            </div>

                            <div class="form-group col-sm-3">
                                <label for="applyEndDate" class="col-md-4 control-label">申报结束日期</label>
                                <div class="col-md-8">
                                    <div class="input-group date form_datetime" data-date-format="yyyy-mm-dd"  data-link-field="applyEndDateLink">
                                        <input id="applyEndDate" class="form-control" type="text" required="true" aria-required="true" th:value="${news!=null and news.applyEndDate!=null }?${#dates.format(news.applyEndDate,'yyyy-MM-dd')}: ''" readonly>
                                        <span class="input-group-addon"><span class="glyphicon glyphicon-remove"></span></span>
                                        <input type="hidden" id="applyEndDateLink" name="applyEndDate" th:value="${news!=null and news.applyEndDate!=null }?${#dates.format(news.applyEndDate,'yyyy-MM-dd')}: ''" />
                                    </div>
                                </div>
                            </div>

                            <div class="form-group col-sm-3">
                                <label for="publishDate" class="col-md-4 control-label">发布日期</label>
                                <div class="col-md-8">
                                    <div class="input-group date form_datetime" data-date-format="yyyy-mm-dd" data-link-field="publishDateLink">
                                        <input id="publishDate" class="form-control" type="text" required="true" aria-required="true" th:value="${news!=null and news.publishDate!=null }?${#dates.format(news.publishDate,'yyyy-MM-dd')}: ''" readonly>
                                        <span class="input-group-addon"><span class="glyphicon glyphicon-remove"></span></span>
                                        <input type="hidden" id="publishDateLink" name="publishDate" th:value="${news!=null and news.publishDate!=null }?${#dates.format(news.publishDate,'yyyy-MM-dd')}: ''" />
                                    </div>
                                </div>
                            </div>

                            <div class="form-group col-md-12">
                                <label class="control-label" for="title">文章标题</label>
                                <input id="title" type="text" class="form-control" placeholder="文章标题" name="title" required="" aria-required="true"
                                       th:value="${news!=null and news.title!=null }?${news.title}: ''"/>
                            </div>

                            <div class="form-group col-md-12">
                                <label class="control-label" for="summary">文章摘要</label>
                                <textarea id="summary"  name="summary" class="form-control" th:text="${news!=null and news.summary !=null}?${news.summary}: ''"></textarea>
                            </div>

                            <div class="form-group col-md-12">
                                <label class="control-label" for="editor">文章正文</label>
                                <textarea id="editor" name="content" th:utext="${news!=null and news.content !=null}?${news.content}: ''"></textarea>
                            </div>

                            <div class="col-md-12" >
                                <div class="text-right">
                                    <button type="submit" class="btn btn-success waves-effect waves-light"><i class="fa fa-check" aria-hidden="true"></i>保存</button>
                                    <a href="javascript:void(0)" onclick="window.history.go(-1);" class="btn btn-warning waves-effect waves-light">
                                        <i class="fa fa-times"></i> <span>取消</span>
                                    </a>
                                </div>
                            </div>
                        </form>
                    </div>

                </div>
                <div th:replace="admin/footer :: footer-content"></div>
            </div>
        </div>
    </div>
</div>
<div th:replace="admin/footer :: footer"></div>

<script th:src="@{//cdn.bootcss.com/jquery-toggles/2.0.4/toggles.min.js}"></script>
<script th:src="@{//cdn.bootcss.com/jquery-validate/1.15.1/jquery.validate.min.js}"></script>
<script th:src="@{//cdn.bootcss.com/jquery-validate/1.15.1/localization/messages_zh.min.js}"></script>
<script th:src="@{/plugin/imageUpload/js/upload.js}"></script>

<script src="/plugin/ckeditor5/translations/zh-cn.js"></script>
<script src="/plugin/ckeditor5/ckeditor.js"></script>
<script src="/plugin/datetimepicker/js/bootstrap-datetimepicker.min.js"></script>
<script src="/plugin/datetimepicker/js/locales/bootstrap-datetimepicker.zh-CN.js"></script>


<script type="text/javascript">
    /*<![CDATA[*/

    $(".form_datetime").datetimepicker({
        format: 'yyyy-mm-dd',
        language: 'zh-CN',
        autoclose:1,
        startDate:new Date(),
        weekStart: 1,
        todayBtn:  1,
        todayHighlight: 1,
        startView: 2,
        minView: 2,
        forceParse: 0
    });

    //富文本start
    class UploadAdapter {
        constructor(loader) {
            this.loader = loader;
        }

        upload() {
            return new Promise((resolve, reject) => {
                const data = new FormData();
                data.append('file', this.loader.file);
                data.append('allowSize', 2);//允许图片上传的大小/兆
                $.ajax({
                    url: '/admin/qiniu/uploadImg',
                    type: 'POST',
                    data: data,
                    dataType: 'json',
                    processData: false,
                    contentType: false,
                    success: function (data) {
                        console.log(data);
                        if (data.code == -1) {
                            resolve({
                                default: data.payload.url
                            });
                        } else {
                            reject(data.msg);
                        }

                    }
                });

            });
        }

        abort() {
        }
    }


    ClassicEditor.create(document.querySelector('#editor'), {
        language: 'zh-cn'
    }).then(editor => {
        //const toolbarContainer = document.querySelector('#a');
        //toolbarContainer.appendChild(editor.ui.view.toolbar.element);

        // 这个地方加载了适配器
        editor.plugins.get('FileRepository').createUploadAdapter = (loader) => {
            return new UploadAdapter(loader);
        }
    }).catch(error => {
        console.error(error);
    })
    //富文本end


    //表单提交
    var tale = new $.tale();
    // validate the comment form when it is submitted
    $('#newsForm').validate({
        submitHandler: function (form) {

            //$('#content-editor').val($('#text').val());
            //$("#newsForm #categories").val($('#multiple-sel').val());
            var params =  $("#newsForm").serialize();
            var url = '/admin/news/save';
            tale.post({
                url:url,
                data:params,
                //contentType:"application/json;charset=utf-8",
                success: function (result) {
                    if (result && result.success) {
                        tale.alertOk({
                            text:'页面保存成功',
                            then: function () {
                                setTimeout(function () {
                                    window.location.href = '/admin/news/list';
                                }, 500);
                            }
                        });
                    } else {
                        tale.alertError(result.msg || '页面文章失败');
                    }
                }
            });
        }
    });

    /*]]>*/
</script>

</body>
</html>